package gui;

import java.awt.CardLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

import rulesControl.Handicap;

/**
 * @author Wojciech Łopata
 *
 */

public class HandicapSelectionPanel extends JPanel
{
	private static final long serialVersionUID = -6902213822662909405L;
	private volatile HandicapType selectedHandicapType;
	private final ButtonGroup handicapSelectionGroup;
	
	private final JComboBox komiComboBox;
	private final JLabel komiLabel;
	private final JPanel komiPanel;
	
	private final JComboBox freeStonesComboBox;
	private final JLabel freeStonesLabel;
	private final JPanel freeStonesPanel;
	
	private final JPanel emptyPanel;
	private final JPanel handicapSettingsPanel;
	
	private class CustomActionListener implements ActionListener
	{
		private final HandicapType handicapType;
		public CustomActionListener(HandicapType handicapType)
		{
			this.handicapType = handicapType;
		}
		@Override
		public void actionPerformed(ActionEvent arg0)
		{
			selectedHandicapType = handicapType;
			
			komiPanel.setVisible(false);
			freeStonesPanel.setVisible(false);
			emptyPanel.setVisible(false);
			
			switch(handicapType)
			{
				case KOMI:
					komiPanel.setVisible(true);
					break;
				case STONES:
					freeStonesPanel.setVisible(true);
					break;
				case NONE:
					emptyPanel.setVisible(true);
					break;
			}
			validate();
		}
		
	}
	
	public HandicapSelectionPanel()
	{
		handicapSelectionGroup = new ButtonGroup();
		setLayout(new GridLayout(0, 1));
		add(new JLabel("Handicap:"));
		for(int i = 0; i < HandicapType.values().length; ++i)
		{	
			JRadioButton box = new JRadioButton(HandicapType.values()[i].toString(), false);
			box.addActionListener(new CustomActionListener(HandicapType.values()[i]));
			handicapSelectionGroup.add(box);
			if(HandicapType.values()[i].equals(HandicapType.getDefaultHandicap()))
				box.setSelected(true);
			add(box);
		}
		selectedHandicapType = HandicapType.getDefaultHandicap();
		komiComboBox = new JComboBox(new Double [] {4.5, 5.5, 6.5, 7.5} );
		komiComboBox.setSelectedIndex(1);
		komiLabel = new JLabel("Komi: ");
		komiPanel = new JPanel();
		komiPanel.setLayout(new FlowLayout());
		komiPanel.add(komiLabel);
		komiPanel.add(komiComboBox);
		
		freeStonesComboBox = new JComboBox(new Integer [] {1, 2, 3, 4, 5, 6, 7, 8, 9} );
		freeStonesLabel = new JLabel("Ilość kamieni: ");
		freeStonesPanel = new JPanel();
		freeStonesPanel.setLayout(new FlowLayout());
		freeStonesPanel.add(freeStonesLabel);
		freeStonesPanel.add(freeStonesComboBox);
		
		emptyPanel = new JPanel();
		handicapSettingsPanel = new JPanel();
		handicapSettingsPanel.setLayout(new CardLayout());
		handicapSettingsPanel.add(emptyPanel);
		handicapSettingsPanel.add(freeStonesPanel);
		handicapSettingsPanel.add(komiPanel);

		add(handicapSettingsPanel);
		setVisible(true);
	}
	
	public Handicap getSelectedHandicap()
	{
		if(selectedHandicapType == null) throw new IllegalStateException("Nie wybrano handicapu.");
		
		switch(selectedHandicapType)
		{
			case KOMI:
				return new Handicap((Double) komiComboBox.getSelectedItem());
			case STONES:
				return new Handicap((Integer) freeStonesComboBox.getSelectedItem());
			default:
				return new Handicap();
		}
	}
	
	
	private enum HandicapType
	{
		KOMI
		{
			@Override
			public String toString()
			{
				return "Punkty komi";
			}
		}, 
		
		STONES
		{
			@Override
			public String toString()
			{
				return "Dodatkowe kamienie";
			}
		},
		
		NONE
		{
			@Override
			public String toString()
			{
				return "Brak";
			}
		};
		
		public static HandicapType getDefaultHandicap()
		{
			return NONE;
		}
	}
}
